package com.soilflow.info.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.NumberUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soilflow.common.domain.*;
import com.soilflow.common.domain.dto.SysArticleDto;
import com.soilflow.common.domain.vo.*;
import com.soilflow.common.excle.DuocaiArticleExcel;
import com.soilflow.common.excle.LandInfoExcel;
import com.soilflow.common.excle.im.InfoExcel;
import com.soilflow.common.utils.DateUtils;
import com.soilflow.info.excelListenner.SysArticleListenner;
import com.soilflow.info.excelListenner.landInfoListenner;
import com.soilflow.info.mapper.LandRentMapper;
import com.soilflow.info.mapper.SysArticleMapper;
import com.soilflow.info.mapper.SysLikeMapper;
import com.soilflow.info.service.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import static com.soilflow.common.utils.PageUtils.startPage;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author party
 * @date 2023-10-09
 */
@Service
public class SysArticleServiceImpl extends ServiceImpl<SysArticleMapper, SysArticle> implements ISysArticleService {
    @Resource
    private SysArticleMapper sysArticleMapper;
    @Autowired
    private ISysCollectService sysCollectService;
    @Autowired
    private ISysLikeService sysLikeService;
    @Resource
    private SysLikeMapper sysLikeMapper;
    @Autowired
    private ISysCategoryService categoryService;
    @Autowired
    private ISysCategoryService2 categoryService2;
    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SysArticle selectSysArticleById(Long id)
    {
        return sysArticleMapper.selectSysArticleById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param sysArticle 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SysArticle> selectSysArticleList(SysArticle sysArticle)
    {
        return sysArticleMapper.selectSysArticleList(sysArticle);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param sysArticle 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSysArticle(SysArticle sysArticle) {
        sysArticle.setCreateTime(DateUtils.getNowDate());
        return sysArticleMapper.insertSysArticle(sysArticle);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param sysArticle 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSysArticle(SysArticle sysArticle)
    {
        sysArticle.setUpdateTime(DateUtils.getNowDate());
        System.out.println("sysArticle.getContent() = " + sysArticle.getContent());
        return sysArticleMapper.updateSysArticle(sysArticle);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysArticleByIds(Long[] ids)
    {
        for (Long id : ids) {
            LambdaUpdateWrapper<SysArticle> wrapper=new LambdaUpdateWrapper<>();
            wrapper.eq(SysArticle::getId,id);
            wrapper.set(SysArticle::getDeleted,1);
            int update = sysArticleMapper.update(null, wrapper);
        }
            return 1;
//        return sysArticleMapper.deleteSysArticleByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysArticleById(Long id)
    {
        return sysArticleMapper.deleteSysArticleById(id);
    }

    /**
     * 查询文章列表，并查出该文章的点赞数据、收藏数据
     * @param
     * @return
     */
    @Override
    public List<SysArticle> selectarticle(SysArticleDto sysArticle) {
        List<SysArticle> list = sysArticleMapper.selectarticle(sysArticle);
        return list;
    }

    @Override
    public List<SysArticleVo> hotArticleList() {
        List<SysArticleVo> articleVos=new ArrayList<>();
        //查询前五条的热门文章
        //根据点赞表查询点赞数量最多的前五条
        List<SysLike> list = sysLikeMapper.hotArticleList();
        //根据文章id获取文章表信息
        for (SysLike like : list) {
//            SysArticleVo articleVo=new SysArticleVo();
//            Long articleId = like.getArticleId();
//            System.out.println("like.getArticleId() = " + like.getArticleId());
//            SysArticle article = this.baseMapper.getArticle(articleId);
//            System.out.println("article = " + article);
            //获取收藏量
//            LambdaQueryWrapper<SysCollect> wrapper=new LambdaQueryWrapper<>();
////            wrapper.eq(SysCollect::getArticleId,articleId);
////            int count = sysCollectService.count(wrapper);
////            System.out.println("count = " + count);
////            article.setViewCount(count);
            //获取点赞量
            LambdaQueryWrapper<SysLike> queryWrapper=new LambdaQueryWrapper<>();
//            queryWrapper.eq(SysLike::getArticleId,articleId);
//            int count1 = sysLikeService.count(queryWrapper);
//            article.setLikeCount(count1);
//            System.out.println("article = " + article);
//            BeanUtils.copyProperties(article,articleVo);
//            articleVos.add(articleVo);
        }
        return articleVos;
    }

    /**
     * 根据分类展示最新文章
     * @return
     * id 分类id
     */
    @Override
    public List<categoryArticleVo> categoryList(Long id) {
            SysCategory category = categoryService.getById(id);
        System.out.println("category = " + category);
            LambdaQueryWrapper<SysArticle> wrapper=new LambdaQueryWrapper<>();
            wrapper.eq(SysArticle::getCategoryId,id);
            wrapper.orderByDesc(SysArticle::getCreateTime);
            wrapper.last("limit 3");
            List<SysArticle> articles = this.baseMapper.selectList(wrapper);
        List<categoryArticleVo> list=new ArrayList<>();
            for (SysArticle article : articles) {
                System.out.println("article = " + article);
                categoryArticleVo categoryArticleVo = new categoryArticleVo();
                categoryArticleVo.setCategoryId(article.getCategoryId());
                categoryArticleVo.setArticleId(article.getId());
                categoryArticleVo.setCategoryName(category.getCategoryName());
                categoryArticleVo.setTitle(article.getTitle());
                list.add(categoryArticleVo);
            }
        return list;
    }

    @Override
    public categorySearchVo categorySerach(String id) {
        LambdaQueryWrapper<SysArticle> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SysArticle::getCategoryId,id);
        wrapper.eq(SysArticle::getDeleted,0);
        List<SysArticle> articles = this.list(wrapper);
        List<Long> articleIds=new ArrayList<>();
        List<String> articleNames=new ArrayList<>();
        for (SysArticle article : articles) {
            articleIds.add(article.getId());
            articleNames.add(article.getTitle());
        }
        SysCategory category = categoryService.getById(id);
        categorySearchVo categorySearchvo=new categorySearchVo();
        categorySearchvo.setCategoryId(category.getId());
        categorySearchvo.setCategoryName(category.getCategoryName());
        categorySearchvo.setArticleId(articleIds);
        categorySearchvo.setTitle(articleNames);
        return categorySearchvo;
    }

    /**
     * 导出
     *
     * @param response
     */
    @Override
    public void exportExcel1(HttpServletResponse response) throws IOException {
        List<DuocaiArticleExcel> articleExcels=this.baseMapper.selectExcel();
        for (DuocaiArticleExcel articleExcel : articleExcels) {
            Long id = articleExcel.getId();
            //获取收藏量
//            LambdaQueryWrapper<SysCollect> wrapper=new LambdaQueryWrapper<>();
//            wrapper.eq(SysCollect::getArticleId,id);
//            int count = sysCollectService.count(wrapper);
//            articleExcel.setViewCount(count);
            //获取点赞量
            LambdaQueryWrapper<SysLike> queryWrapper=new LambdaQueryWrapper<>();
//            queryWrapper.eq(SysLike::getArticleId,id);
            int count1 = sysLikeService.count(queryWrapper);
            System.out.println("count1 = " + count1);
            articleExcel.setLikeCount(count1);
        }
        EasyExcel.write(response.getOutputStream(), DuocaiArticleExcel.class).sheet().doWrite(articleExcels);
    }

    @Override
    public void importExcel(MultipartFile file, ISysArticleService sysArticleService,ISysCategoryService categoryService) throws IOException{
        EasyExcel.read(file.getInputStream(), InfoExcel.class,new SysArticleListenner(sysArticleService,categoryService)).doReadAll();
    }

    @Override
    public List<LandmarkVo> projectJingweidu() {
        List<LandmarkVo> vos=new ArrayList<>();
        List<SysArticle> articles = this.baseMapper.projectJingweidu();
        int is=0;
        for (SysArticle article : articles) {
            is=is+1;
            LandmarkVo landmarkVo = new LandmarkVo();
            landmarkVo.setId(is);
            if (article.getLatitude()!=null) {
                landmarkVo.setLatitude(Double.parseDouble(article.getLatitude()));
            }
            if (article.getLongitude()!=null){
                landmarkVo.setLongitude(Double.parseDouble(article.getLongitude()));
            }
            landmarkVo.setIconPath("https://staticimg.weikedashi.net/duocaidatonghu/live.png");
            landmarkVo.setWidth("20");
            landmarkVo.setHeight("20");

            callout callout = new callout();
            callout.setContent(article.getTitle());
            callout.setColor("#000");
            callout.setFontSize(10);
            callout.setPadding(5);
            callout.setBgColor("#d2fdff");
            callout.setBorderColor("#466dc8");
            callout.setBorderRadius(5);
            callout.setDisplay("ALWAYS");
            landmarkVo.setCallout(callout);
            vos.add(landmarkVo);
        }
        return vos;
    }

    @Override
    public SysArticle searchArticle(String title) {
        LambdaQueryWrapper<SysArticle> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SysArticle::getTitle,title);
        SysArticle one = this.getOne(wrapper);
        return one;
    }
}
